package cn.wk.basic.service.impl;

import cn.wk.basic.mapper.BaseMapper;
import cn.wk.basic.query.BaseQuery;
import cn.wk.basic.service.IBaseService;
import cn.wk.basic.utils.PageBean;
import cn.wk.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    //注入mapper
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public Long query(BaseQuery query) {
        return baseMapper.query(query);
    }

    @Override
    public PageBean<T> queryAll(BaseQuery query) {
        //先查询总条数
        Long count = baseMapper.query(query);
        //判断总数是否为null或者为0，就是说没有数据
        if(count==null || count<=0){
            return new PageBean<>();
        }
        //有数据的话，就去查询所有数据，并分页
        List<T> list = baseMapper.queryData(query);
        //然后将总条数和数据返回
        return new PageBean<T>(count,list);
    }

    @Override
    @Transactional
    public void delBatch(List<T> t) {
        baseMapper.delBatch(t);
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }
}
